package com.smarthome.core.operationservicemanager.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.smarthome.base.service.ShCompanyLaundryService;
import com.smarthome.base.service.ShResidenceLaundryService;
import com.smarthome.core.operationservicemanager.CompanyLaundryService;
import com.smarthome.domain.bean.ShCompanyLaundry;
import com.smarthome.domain.bean.ShResidenceLaundry;
import com.smarthome.domain.vo.boss.Combobox;
import com.smarthome.domain.vo.boss.DataGrid;
import com.smarthome.domain.vo.boss.PageHelper;

/**
 * 
* @ClassName: CompanyLaundryServiceImpl
* @Description: 洗衣店service接口实现类
* @author cyj
* @date 2016年3月22日 下午10:06:49
*
 */
@Service("companyLaundryService")
public class CompanyLaundryServiceImpl implements CompanyLaundryService {

	@Resource
	private ShCompanyLaundryService shCompanyLaundryService;
	@Resource
	private ShResidenceLaundryService shResidenceLaundryService;

	@Override
	public ShCompanyLaundry getById(Long id) {
		return shCompanyLaundryService.selectEntryById(id);
	}

	@Override
	public DataGrid qeuryDataGrid(ShCompanyLaundry companyLaundry, PageHelper page) {
		return shCompanyLaundryService.quetyDataGrid(companyLaundry, page);
	}

	@Override
	public Integer saveOrUpdate(ShCompanyLaundry companyLaundry) {
		return shCompanyLaundryService.saveOrUpdate(companyLaundry);
	}

	@Override
	public Integer delete(Long id) {
		return shCompanyLaundryService.deleteByIds(id);
	}

	@Override
	public List<Combobox> getCombobox(Long residenceId) {
		List<Combobox> result = new ArrayList<Combobox>();
		ShResidenceLaundry residenceLaundry = new ShResidenceLaundry();
		residenceLaundry.setResidenceId(residenceId);
		
		List<Long> laundryIdList = shResidenceLaundryService.getLaundryIdList(residenceLaundry);
		
		
		if(laundryIdList != null && !laundryIdList.isEmpty()) {
			Long[] laundryIds = laundryIdList.toArray(new Long[laundryIdList.size()]);
			
			List<ShCompanyLaundry> laundrylist = shCompanyLaundryService.selectEntryListByIds(laundryIds);
			
			int length = laundrylist.size();
			for(int i = 0;i < length;i++) {
				if(laundrylist.get(i).getStatus() != null && laundrylist.get(i).getStatus().equals(1)) {
					result.add(new Combobox(laundrylist.get(i).getId().toString(), laundrylist.get(i).getName()));
				}
			}
		}
		return result;
	}

	@Override
	public boolean checkName(ShCompanyLaundry companyLaundry) {
		if(companyLaundry == null || StringUtils.isBlank(companyLaundry.getName()) || StringUtils.isBlank(companyLaundry.getAddr())) {
			return false;
		}
		ShCompanyLaundry laundry = new ShCompanyLaundry();
		laundry.setName(companyLaundry.getName());
		laundry.setAddr(companyLaundry.getAddr());
		List<ShCompanyLaundry> list = shCompanyLaundryService.selectEntryList(laundry);
		if(list == null || list.isEmpty()) {
			return true;
		}else {
			if(list.get(0).getId().equals(companyLaundry.getId())) {
				return true;
			}else {
				return false;
			}
		}
	}

	
	

}
